import {apiIheadPage, QmsmgDel} from '@/api/url/checkModule';
import router from '@/router';
import { isNullPm, routerJump } from '@/utils/baseMethods';
import { reactive } from 'vue';
import notification from "@arco-design/web-vue/es/notification";

export default function importConfig() {
  const base = reactive<any>({
    pagination: {
      defaultPageSize: 10,
      total: 100, //总条数
      current: 1,
      pageSize: 1000,
      showTotal: true,
      showPageSize: true,
      pageSizeOptions: [10, 20, 30, 40, 50],
      hideOnSinglePage: true,
    },
    tabelList: null,
    rowSelection: {
      type: 'checkbox',
      showCheckedAll: true,
    },
    idsList: null,
    optInForm: {
      type: 'restockInspect',
      ta001: null,
      ta002: null,
      ta006: null,
    },
  });

  const init = () => {
    getTable();
  };
  const getTable = () => {
    const steam = {
      pagesize: base.pagination.pageSize,
      page: base.pagination.current,
    };
    Object.keys(base.optInForm).map((res: any) => {
      if (!isNullPm(base.optInForm[res])) steam[res] = base.optInForm[res];
    });
    apiIheadPage(steam).then((res: any) => {
      base.tabelList = res.data.records.map((item: any) => {
        item.result = item.result == 'Y' ? "合格" : "不合格" ;
        return item;
      });
      base.tabelList.map((rea: any) => {
        rea.key = rea.uid;

      });
      base.pagination.total = res.data.total;
    });
  };

  const columns = [
    {
      title: '工单单别',
      dataIndex: 'ta001',
      align: 'center',
    },
    {
      title: '工单单号',
      dataIndex: 'ta002',
      align: 'center',
    },
    {
      title: '工艺',
      dataIndex: 'ta004',
      align: 'center',
    },
    {
      title: '工艺名称',
      dataIndex: 'ta024',
      align: 'center',
    },
    {
      title: '品号',
      dataIndex: 'ta006',
      align: 'center',
    },
    {
      title: '品名',
      dataIndex: 'mb002',
      align: 'center',
    },
    {
      title: '规格',
      dataIndex: 'mb003',
      align: 'center',
    },
    {
      title: '创建时间',
      dataIndex: 'creatTime',
      align: 'center',
    },
    {
      title: '检验时间',
      dataIndex: 'startTime',
      align: 'center',
    },
    {
      title: '抽检数',
      dataIndex: 'udf66',
      align: 'center',
    },
    {
      title: '样本数',
      dataIndex: 'udf88',
      align: 'center',
    },
    {
      title: '检验场地',
      dataIndex: 'udf06',
      align: 'center',
    },
    {
      title: '检验结果',
      dataIndex: 'result',
      align: 'center',
    },
    {
      title: '操作',
      slotName: 'optional',
      align: 'center',
    },
  ];

  const collection = reactive([
    { label: '公司名称', value: '', placeholder: '请输入公司名称', isInput: true },
    { label: '创建人', value: '', placeholder: '请输入公司名称', isSelect: true },
    { label: '供应商 ID', value: '', placeholder: '请输入公司名称', isRangePicker: true },
    { label: '供应商名称', value: '', placeholder: '请输入公司名称', isInput: true },
    { label: '负责人', value: '', placeholder: '请输入公司名称', isInput: true },
    { label: '负责人号码', value: '', placeholder: '请输入公司名称', isInput: true },
  ]);

  const handlePageChange = (page: number) => {
    base.pagination.current = page;
  };
  const handlePageSizeChange = (pageSize: number) => {
    base.pagination.pageSize = pageSize;
  };

  /**
   * @exports viewDetails 查看详情单
   * @param param 品号
   */
  const viewDetails = (param) => {
    // console.log(param)
    router.push({ path: '/inspectionPolicyDetails', query: { uid: param.uid } });
  };

  /**
   * @exports viewInspect 调起检验单
   */
  const viewInspect = () => {};

  /**
   *
   */
  const printWindow = (param) => {
    getIdsPm();
  };

  const rowSelectBow = (param) => {
    base.idsList = param;

    console.log(param);
  };

  const selectAll = (param) => {
    base.idsList = [];
    if (param) {
      base.tabelList.map((res: any) => {
        base.idsList.push(res.uid);
      });
    }
  };

  /**
   * @exports getIdsPm 批量获取ids 数据
   */
  const getIdsPm = () => {
    routerJump('/printTemplate', { ids: JSON.stringify(base.idsList) });
    // let steam = {
    //   ids: [35,36]
    // }
    // console.log(steam)
    // apiIheadAlllist(steam).then((res: any) => {
    //   console.log(res, '==>')
    // })
  };

  const printPom = () => {
    routerJump('/BOMList');
  };

  // 重置
  const reset = () => {
    base.optInForm.mb002 = "";
    base.optInForm.ta006 = "";
    base.optInForm.ta001 = "";
    base.optInForm.ta002 = "";
    init();
  }

  //删除
  const del=(record)=>{
    QmsmgDel(record.uid).then(res=>{
      notification.info(res.data);
      if(res.code==200){
        getTable();
      }
    });
  };

  return {
    columns,
    collection,
    handlePageChange,
    handlePageSizeChange,
    base,
    viewDetails,
    viewInspect,
    init,
    printWindow,
    rowSelectBow,
    selectAll,
    printPom,
    reset,
    del,
  };
}
